package com.myplaces.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.geocode.Geocoder;
import com.google.gwt.maps.client.geocode.LocationCallback;
import com.google.gwt.maps.client.geocode.Placemark;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.TextBox;

public class GotoPanel extends DecoratorPanel {

	public GotoPanel(final MapWidget map) {
		FlowPanel content = new FlowPanel();
		setWidget(content);
		final TextBox tb = new TextBox();
		tb.setText("Enter location here...");
		content.add(tb);
		Button b = new Button("Go", new ClickHandler() {
			@Override
			public void onClick(ClickEvent arg0) {
				Geocoder g = new Geocoder();
				g.getLocations(tb.getText(), new LocationCallback() {

					@Override
					public void onSuccess(JsArray<Placemark> locations) {
						Placemark p = locations.get(0);
						Marker m = new Marker(p.getPoint());
						map.setCenter(p.getPoint());
						map.setZoomLevel(p.getAccuracy());
						map.addOverlay(m);
					}

					@Override
					public void onFailure(int statusCode) {
						GWT.log("failed " + statusCode, null);

					}
				});
			}
		});
		content.add(b);
	}
}
